﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using TextEditor.TextDocument.Control;

namespace TextEditor.TextDocument.Action
{
    public class A_FindOrReplace : A_Action
    {
        public A_FindOrReplace()
        {
            this.HotKey = System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F;
        }
        ucFindOrReplace p = null;
        public override string ActionName()
        {
            return "diagnose";
        }

        public override bool Execute()
        {
            UIExecute();
            return true;
        }
        frmFindReplace ffr = null;
        public override bool UIExecute()
        {
            int add = 0;
            if (p == null)
            {
                p = new ucFindOrReplace();
                p.Dock = DockStyle.Bottom;
                p.OnFind += new ucFindOrReplace.Find(p_OnFind);
                p.OnReplace += new ucFindOrReplace.Replace(p_OnReplace);
                this.OwnerDocument.Menus.PnlMenus.Controls.Add(p);
                add = 1;
                this.OwnerDocument.Menus.PnlMenus.Height += 29;
            }
            if (add == 0)
            {
                if (p.Visible == false)
                {
                    p.Visible = true;
                    this.OwnerDocument.Menus.PnlMenus.Height += 29;
                }
            }
            //if (ffr == null)
            //{
            //    ffr = new frmFindReplace(this.OwnerDocument);
            //    ffr.TopMost = true;
            //    ffr.SendToBack();

            //}
            //ffr.Show();
            return true;
        }

        void p_OnReplace(string s1, string s2)
        {
            this.OwnerDocument.OwnerControl.Focus();
            this.OwnerDocument._Replace(s1, s2);
        }

        void p_OnFind(object o, string s)
        {
            this.OwnerDocument.OwnerControl.Focus();
            this.OwnerDocument._Find(s);
        }

        public override bool isEnable()
        {
            return base.myOwnerDocument.CanModify();
        }
    }
}
